package ru.mipt.simt;

import java.util.ArrayList;

public class Road
{
	protected Source source = new Source(16, 2, 1, 1, 7);

	protected ArrayList<Car> cars = new ArrayList<Car>();
	protected double length = 1000.;
	protected double t = 0.;

	public void update(double t)
	{
		Car newCar = source.getCar(t);
		if (newCar != null)
		{
			//TODO delete
			newCar.vs = newCar.v0;
			
			newCar.s = 0.;
			newCar.z = 0.;
			cars.add(newCar);
		}
		double dt = t - this.t;
		int n = cars.size();

		for (int i = 0; i < n; i++)
		{
			Car car = cars.get(i);
			car.s += car.vs * dt;
			car.vs += car.as * dt;
			// TODO update car.as
			car.z += car.vz * dt;
			car.vz += car.az * dt;
			// TODO update car.az
		}
		
		n = 0;
		while (n < cars.size())
		{
			Car car = cars.get(n);
			if (car.s > length)
			{
				cars.remove(n);
				n++;
			} else
				break;
		}

		this.t = t;
		
		measure(t);
	}

	private void measure(double t)
	{
		
	}
	
	public ArrayList<Car> getCars()
	{
		return cars;
	}
}
